<?php
namespace haibao\erp\data\product;

use \haibao\frame\data\query\Condition;
use \haibao\erp\model\filter\product\ProductCategoryProperty as PropertyFilter;
use \haibao\erp\model\filter\product\ProductCategory as CategoryFilter;

class ProductCategoryProperty extends \haibao\erp\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\erp\model\data\product\ProductCategoryProperty');
	}
	
	/**
	 * @param $propertyFilter \haibao\erp\model\filter\product\ProductCategoryProperty
	 */
	public function getAll($propertyFilter){
		return parent::getAll($propertyFilter);
	}
	
	/**
	 * 删除属性
	 */
	public function deleteProperty($propertyId,$arrSet){
		parent::updateById($propertyId, $arrSet);
	}
	
	/**
	 * 获取当前分类最大的排序
	 */
	public function getMaxOrder($categoryId){
		$max = null;
		$sql = "select max(OrderNo) as max from ProductCategoryProperty where categoryId=%s AND Status = %s";
		$result  = $this->query($sql,array(
				$categoryId,\haibao\erp\model\data\product\ProductCategoryProperty::STATUS_ENABLE
				));
		
		while ($result->fetch_assoc()){
			$max = $result->getData('max');
		}
		return $max;
	}
	/**
	 * 根据属性id查找
	 */
	public function getPropertyById($propertyId){
		$propertyFilter = new PropertyFilter();
		$categoryFilter = new CategoryFilter();
		$propertyFilter->select(array(
				PropertyFilter::CONDITION_FIELD_NAME_ID,
				PropertyFilter::CONDITION_FIELD_NAEM_ORDERNO,
				PropertyFilter::CONDITION_FIELD_NAME_CATEGORY_ID,
				PropertyFilter::CONDITION_FIELD_NAME_DATA_TYPE,
				PropertyFilter::CONDITION_FIELD_NAME_DEFAULT_VALUE,
				PropertyFilter::CONDITION_FIELD_NAME_PROPERTY_NAME,
				PropertyFilter::CONDITION_FIELD_NAME_SELECT_DATA_VALUE
				));
		$categoryFilter->select(array(
				CategoryFilter::CONDITION_FIELD_NAME_CATEGORY_NAME,
				CategoryFilter::CONDITION_FIELD_NAME_ID
				));
		$propertyFilter->leftJoin($categoryFilter,null,PropertyFilter::CONDITION_FIELD_NAME_CATEGORY_ID,CategoryFilter::CONDITION_FIELD_NAME_ID,PropertyFilter::CONDITION_FIELD_NAME_CATEGORY,false);
		$propertyFilter->where(PropertyFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\erp\model\data\product\ProductCategoryProperty::STATUS_ENABLE);
		$propertyFilter->where(PropertyFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_EQUAL, $propertyId);
		return $this->getOne($propertyFilter);
	}
	/**
	 * @param \haibao\erp\model\data\product\ProductCategoryProperty $categoryPropertyModel
	 */
	public function add($model){
		self::beginTranscation();
		try{
			parent::add($model);
		}catch (\Exception $e){
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	/**
	 * @param array()\haibao\erp\model\data\product\ProductCategoryProperty $categoryPropertyModels
	 */
	public function addAll($models){
		self::beginTranscation();
		try{
			foreach($models as $key=>$model){
				parent::add($model);
				$models[$key] = $model;
			}
		
		}catch (\Exception $e){
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	/**
	 * 
	 * @param \haibao\erp\model\data\product\ProductCategoryProperty $categoryPropertyModel
	 */
	public function save($model){
		self::beginTranscation();
		try{
			parent::updateById($model->Id, array(
					PropertyFilter::CONDITION_FIELD_NAEM_ORDERNO =>$model->OrderNo,
					PropertyFilter::CONDITION_FIELD_NAME_DATA_TYPE => $model->DataType,
					PropertyFilter::CONDITION_FIELD_NAME_DEFAULT_VALUE => $model->DefaultValue,
					PropertyFilter::CONDITION_FIELD_NAME_PROPERTY_NAME => $model->PropertyName,
					PropertyFilter::CONDITION_FIELD_NAME_SELECT_DATA_VALUE =>$model->SelectDataValue,	
					));
		}catch (\Exception $e){
			$this->rollback();
			throw $e;
		}
		$this->endTranscation();
	}
	
	/**
	 * 删除子孩子属性
	 * @param  int $id
	 */
	public function deleteSnycSubProperty($id){
		$property = $this->getOneById($id);
		$propertyName = $property->PropertyName;
		
		$propertyFilter = new PropertyFilter();
		$propertyFilter->where(PropertyFilter::CONDITION_FIELD_NAME_PROPERTY_NAME, Condition::CONDITION_EQUAL, $propertyName);
		$propertyFilter->where(PropertyFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $id);
		
		parent::remove($propertyFilter);
	}
	
}
